package com.ywu.api.standard.api;

import com.ywu.api.standard.entity.ApiRouterEntity;
import com.ywu.common.result.CommonResult;
import com.ywu.api.standard.dto.ApiRouterDto;
import com.ywu.api.standard.service.ApiRouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName GatewayRouterApi
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/1/28 18:14
 * @Version 1.0
 **/
@RestController
@RequestMapping("/ywu/api")
public class ApiRouterApi {
    @Autowired
    private ApiRouterService apiRouterService;

    /**
     * @Author GroundDemo
     * @Description 刷新路由
     * @Date 21:36 2024/2/15
     * @Param []
     * @return com.ywu.blog.common.result.CommonResult<java.lang.Object>
     **/
    @GetMapping("/router.refresh.info")
    public CommonResult<Object> refresh() {
        apiRouterService.refreshRoute();
        return CommonResult.success(null);
    }

    /**
     * @Author GroundDemo
     * @Description 添加路由
     * @Date 21:37 2024/2/15
     * @Param [apiRouterDto]
     * @return com.ywu.blog.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/router.add.info")
    public CommonResult<Object> add(@RequestBody @Valid ApiRouterDto apiRouterDto) {
        return apiRouterService.addRoute(apiRouterDto);
    }

    /**
     * @Author GroundDemo
     * @Description 更新路由
     * @Date 21:37 2024/2/15
     * @Param [apiRouterDto]
     * @return com.ywu.blog.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/router.update.info")
    public CommonResult<Object> update(@RequestBody @Valid ApiRouterDto apiRouterDto) {
        return apiRouterService.updateRoute(apiRouterDto);
    }

    /**
     * @Author GroundDemo
     * @Description 删除路由
     * @Date 21:37 2024/2/15
     * @Param [apiRouterDto]
     * @return com.ywu.blog.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/router.delete.info")
    public CommonResult<Object> delete(@RequestBody ApiRouterDto apiRouterDto) {
        return apiRouterService.deleteRoute(apiRouterDto);
    }

    /**
    * @Author GroundDemo
    * @Description 根据条件查询路由列表
    * @Date 21:21 2024/2/28
    * @Param [apiRouterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/api.route.info")
    public CommonResult<Object> apiRouteInfoQuery(@RequestBody ApiRouterDto apiRouterDto) {
        return apiRouterService.apiRouteInfoQuery(apiRouterDto);
    }

    /**
    * @Author GroundDemo
    * @Description 查询API的redis缓存
    * @Date 22:03 2024/2/29
    * @Param [apiRouterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/api.route.redis.cache.info")
    public CommonResult<Object> apiRouteRedisCacheInfo(@RequestBody ApiRouterDto apiRouterDto) {
        return apiRouterService.apiRouteRedisCacheInfo(apiRouterDto);
    }

    /**
     * @Author GroundDemo
     * @Description 根据条件查询路由
     * @Date 21:21 2024/2/28
     * @Param [apiRouterDto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/api.route.info.for.spi")
    public CommonResult<ApiRouterEntity> apiRouteInfoQueryForSpi(@RequestBody ApiRouterDto apiRouterDto) {
        return apiRouterService.apiRouteInfoQueryForSpi(apiRouterDto);
    }

    /**
    * @Author GroundDemo
    * @Description 更新API租户共享状态
    * @Date 14:23 2024/8/18
    * @Param [apiRouterDto]
    * @return com.ywu.common.result.CommonResult<com.ywu.api.standard.entity.ApiRouterEntity>
    **/
    @PostMapping("/api.update.share.api.info")
    public CommonResult<ApiRouterEntity> apiRouteUpdateShareInfo(@RequestBody ApiRouterDto apiRouterDto) {
        return apiRouterService.apiRouteUpdateShareInfo(apiRouterDto);
    }

    /**
    * @Author GroundDemo
    * @Description 查询所有注册的api信息 - 网关调用
    * @Date 19:04 2025/3/17
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.api.standard.entity.ApiRouterEntity>>
    **/
    @PostMapping("/api.route.query.all.routes.spi")
    public CommonResult<List<ApiRouterEntity>> queryAllRoutesSpi() {
        return apiRouterService.queryAllRoutesSpi();
    }

    /**
    * @Author GroundDemo
    * @Description 批量根据code查询数据
    * @Date 14:16 2025/3/24
    * @Param [apiRouterDto]
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.api.standard.entity.ApiRouterEntity>>
    **/
    @PostMapping("/api.query.by.code.for.spi")
    public CommonResult<List<ApiRouterEntity>> queryApiByCode(@RequestBody List<ApiRouterDto> apiRouterDto) {
        return apiRouterService.queryApiByCode(apiRouterDto);
    }
}